﻿using System;
using Model;

namespace DAL.Repositories
{
    public interface IUnitOfWork
    {
        GenericRepository<INVENTORY> InventoryRepository { get; }
        GenericRepository<USERS> UsersRepository { get; }
        GenericRepository<MBEW> MbewRepository { get; }
        GenericRepository<MARA> MaraRepository { get; }
        GenericRepository<MARD> MardRepository { get; }
        GenericRepository<PHANTOMS> PhantomsRepository { get; }
        GenericRepository<ENTRIES> EntryRepository { get; }
        GenericRepository<LABELS> LabelRepository { get; }
        GenericRepository<MASTERFILE> MasterFileRepository { get; }

        void Save(string UserName);
        void Dispose(bool disposing);
        void Dispose();
    }

    public class UnitOfWork : IUnitOfWork
    {
        private readonly GenericContext context = new GenericContext();
        private GenericRepository<INVENTORY> inventoryRepository;
        private GenericRepository<USERS> usersRepository;
        private GenericRepository<MBEW> mbewRepository;
        private GenericRepository<MARA> maraRepository;
        private GenericRepository<MARD> mardRepository;
        private GenericRepository<PHANTOMS> phantomsRepository;
        private GenericRepository<CONVERSIONTABLE> conversionTableRepository;
        private GenericRepository<ENTRIES> entryRepository;
        private GenericRepository<LABELS> labelRepository;
        private GenericRepository<MASTERFILE> masterFileRepository;

        public GenericRepository<INVENTORY> InventoryRepository
        {
            get { return inventoryRepository ?? (inventoryRepository = new InventoryRepository(context)); }
        }

        public GenericRepository<USERS> UsersRepository
        {
            get { return usersRepository ?? (usersRepository = new GenericRepository<USERS>(context)); }
        }

        public GenericRepository<MBEW> MbewRepository
        {
            get { return mbewRepository ?? (mbewRepository = new GenericRepository<MBEW>(context)); }
        }

        public GenericRepository<MARA> MaraRepository
        {
            get { return maraRepository ?? (maraRepository = new GenericRepository<MARA>(context)); }
        }

        public GenericRepository<MARD> MardRepository
        {
            get { return mardRepository ?? (mardRepository = new GenericRepository<MARD>(context)); }
        }

        public GenericRepository<PHANTOMS> PhantomsRepository
        {
            get { return phantomsRepository ?? (phantomsRepository = new GenericRepository<PHANTOMS>(context)); }
        }

        public GenericRepository<LABELS> LabelRepository
        {
            get { return labelRepository ?? (labelRepository = new GenericRepository<LABELS>(context)); }
        }

        public GenericRepository<MASTERFILE> MasterFileRepository
        {
            get { return masterFileRepository ?? (masterFileRepository = new GenericRepository<MASTERFILE>(context)); }
        }

        public GenericRepository<CONVERSIONTABLE> ConversionTableRepository
        {
            get
            {
                return conversionTableRepository ??
                       (conversionTableRepository = new GenericRepository<CONVERSIONTABLE>(context));
            }
        }

        public GenericRepository<ENTRIES> EntryRepository
        {
            get { return entryRepository ?? (entryRepository = new GenericRepository<ENTRIES>(context)); }
        }

        public void Save(string UserName)
        {
            context.UserName = UserName;
            context.SaveChanges();
        }

        private bool disposed;

        public virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
